//package cn.mw.cmdb.service.interceptors;
//
//import cn.mw.cmdb.entity.InstanceInfo;
//import cn.mw.cmdb.entity.InventoryWorkInfo;
//import cn.mw.cmdb.exception.ProcessInterceptorException;
//import cn.mw.cmdb.processor.BusinessOperationService;
//import cn.mw.cmdb.service.ModelService;
//import cn.mw.cmdb.service.ServiceAction;
//import cn.mw.cmdb.service.impl.CmdbServiceManage;
//import cn.mw.cmdb.service.impl.InstanceServiceImpl;
//import cn.mw.cmdb.service.impl.InventoryWorkInfoServiceImpl;
//import cn.mw.microMonitorCommon.annotation.aop.FeatureFlow;
//import cn.mw.microMonitorCommon.entity.Constant;
//import cn.mw.microMonitorCommon.util.ListMapObjUtils;
//import cn.mw.microMonitorCommon.util.SpringUtils;
//import com.alibaba.fastjson2.JSON;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.ibatis.annotations.Param;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//
//import java.util.Arrays;
//import java.util.HashMap;
//import java.util.List;
//import java.util.Map;
//
///**
// * @Author shenwenyi
// * @Date 2024/5/9 17:36
// * @PackageName:cn.mw.cmdb.service.interceptors
// * @ClassName: InventoryWorkProcessInterceptor
// * @Description: TODD
// * @Version 1.0
// */
//@Service
//@Slf4j
//public class InventoryWorkProcessInterceptor implements ServiceInterceptor {
//
//    private static final String ID = "inventory";
//    private CmdbServiceManage cmdbServiceManage;
//    @Autowired
//    private BusinessOperationService businessOperationService;
//
//    public InventoryWorkProcessInterceptor(CmdbServiceManage cmdbServiceManage) {
//        this.cmdbServiceManage = cmdbServiceManage;
//    }
//
//    @Override
//    public boolean intercept(Object obj, ServiceAction action) throws Exception {
//        InventoryWorkProcessInterceptor interceptor = SpringUtils.getBean(InventoryWorkProcessInterceptor.class);
//        InventoryWorkInfoServiceImpl service = (InventoryWorkInfoServiceImpl) cmdbServiceManage.getModelService(InventoryWorkInfoServiceImpl.ID);
//        try {
//            boolean flag = false;
//            Object ret = new Object();
//            if (obj instanceof InventoryWorkInfo) {
//                switch (action) {
//                    case add:
//                        //验证参数,并设置线程变量
////                        service.addAndUpdInstanceValidate((InstanceInfo) obj, true);
//                        ret = interceptor.doAdd((InventoryWorkInfo) obj, null,service);
//                        flag = true;
//                        break;
//                    case update:
//                        //验证参数,并设置线程变量
////                        service.addAndUpdInstanceValidate((InstanceInfo) obj, false);
//                        ret = interceptor.doUpdate((InventoryWorkInfo) obj, null);
//                        flag = true;
//                        break;
//                }
//            }
//            if (ret instanceof Map && ((Map) ret).containsKey(Constant.MSG)) {
//                throw new ProcessInterceptorException();
//            }
//            if (flag) {
//                log.info("记录拦截器实例操作日志 ...");
//                businessOperationService.executeBusinessInstanceOperation(obj, ret, action);
//                return true;
//            }
//            return false;
//        } catch (Exception e) {
//            throw e;
//        } finally {
//            //清空线程变量,防止内存泄漏
//            log.info("clean InstanceServiceImpl thread local");
//            service.removeAddAndUpdInstanceContextThreadLocal();
//        }
//    }
//    @FeatureFlow(methodName = "新增盘点任务")
//    private Object doAdd(InventoryWorkInfo param, @Param("processId") String processId,InventoryWorkInfoServiceImpl service) throws Exception  {
//        Object add = service.add(param);
//        Map<String, Object> map = new HashMap<>();
////        List<Map> mapList = ListMapObjUtils.convertList((List) add);
//        map.put("processId", processId);
//        map.put("resultData", add);
//        log.info("新增新增盘点任务流程返回信息：{}", JSON.toJSONString(map));
//        return map;
//    }
//
//    @FeatureFlow(methodName = "修改盘点任务")
//    private Object doUpdate(InventoryWorkInfo param, @Param("processId") String processId) throws Exception {
//        InventoryWorkInfoServiceImpl service = (InventoryWorkInfoServiceImpl) cmdbServiceManage.getModelService(InventoryWorkInfoServiceImpl.ID);
//        Object update = service.update(param);
//        Map<String, Object> map = new HashMap<>();
//        Map infoMap = ListMapObjUtils.beanToMap(param);
//        map.put("resultData", Arrays.asList(infoMap));
//        map.put("processId", processId);
//        log.info("修改实例流程返回信息：{}", JSON.toJSONString(map));
//        return map;
//    }
//}